package ovm.management.security;

import javax.persistence.*;
import ovm.management.AbstractManager;

import java.util.List;

public class AuthorizationChecker extends AbstractManager {
    
    public static int checkAuthorization (String login, String password) {
        String passwordHash = MD5Hasher.getMD5Hash(password);

        Query query = em.createQuery("SELECT rule FROM UsersEntity WHERE login = :login and password = :password");
        query.setParameter("login", login);
        query.setParameter("password", passwordHash);

        List list = query.getResultList();

        if (list.size() == 0) {
            return -1;
        }

        return (Integer) list.get(0);
    }
    
}
